$(document).ready(function(){
    $.ajaxSetup({
        cache: false,
        type: 'POST',
        dataType: 'json',
        error: function () {
            alert('Um erro de comunicação com o servidor ocorreu!');
            $('#ajax-ldr').fadeOut('fast');
        }
    });
    $("#anterior").click(function(){var pag = $("#pagina").val();if (pag>0){pag--};$("#pagina").val(pag);$('<form>').submit();return false;});
    $("#proximo").click(function(){var pag = $("#pagina").val();pag++;$("#pagina").val(pag);$('<form>').submit();return false;});

    $('.tips').tipsy({
        title:'obs',
        gravity: 'w',
        delayIn:1000,
        delayOut:500,
        html:true
    });
    $('.tips2').tipsy({
        title:'obs',
        gravity: 'e',
        delayIn:1000,
        delayOut:500,
        html:true
    });

    $('.backlog').change(function(){
        var chamada = $(this).attr('chamada');
        var backlog = $(this).val();
        if(chamada!=''){
            $.ajax({
                url: "/backlogs/addpend",
                processData: false,
                data: 'chamada_id='+chamada+'&backlog_id='+backlog,
                beforeSend: function (){
                    var pos = $('#backlog_'+chamada).position();
                    var width = $('#backlog_'+chamada).width();
                    $("#ajax-ldr").css( {
                        "left": (pos.left+width+5) + "px",
                        "top":pos.top + "px"
                    } ).fadeIn('fast');

                },
                success: function(msg){
                    $('#idcontato option').remove();
                    $('#idcontato').append("<option value=''>Selecione o Contato...</option>")
                    for (i = 0; i < msg.length; i++) {
                        $('#idcontato').append("<option value='"+msg[i].idcontato+"'>"+msg[i].dsnome+"</option>")
                    }
                    $('#ajax-ldr').fadeOut('fast');
                }
            });
        }
    });
    $('#idsistema').change(function(){
        if ($('#idsistema').val()!=''){
            $.ajax({
                    url: "/modulos/index",
                    processData: false,
                    data: 'sistema='+$('#idsistema').val(),
                    beforeSend: function (){
                        var pos = $('#idsistema').position();
                        var width = $('#idsistema').width();
                        $("#ajax-ldr").css( {
                            "left": (pos.left+width+5) + "px",
                            "top":pos.top + "px"
                        } ).fadeIn('fast');

                    },
                    success: function(msg){
                        $('#idmodulo option').remove();
                        $('#idmodulo').append("<option value=''>Selecione o Módulo...</option>")
                        for (i = 0; i < msg.length; i++) {
                            $('#idmodulo').append("<option value='"+msg[i].id+"'>"+msg[i].descricao+"</option>")
                        }
                        $('#ajax-ldr').fadeOut('fast');
                    }
                });
        };
    });
});